<?php
/*
 * Created on 08.04.2007
 * @filesource: admin.schichtplan.php
 * @author: Nikolai Plath
 */

   defined( '_VALID_MOS' ) or die( 'Direct Access to this script is not allowed');
   require_once($mainframe->getPath('class'));
   require_once($mainframe->getPath('admin_html'));
   $id = mosGetParam( $_REQUEST, 'cid', array(0) );
   if (!is_array( $id )) {
      $id = array(0);
   }
   if ((!$act || $act == 'all') && !$task)
      $task="listItems";
   switch ($task) {
      case "listItems":
      	listItems($option);
      	break;
      case "edit":
      	edit($option, $id[0]);
      	break;
     //case "new": edit($option, ''); break;
     case "save": save($option); break;
     //case "delete": delete($option, $id); break;
     case "publish": publish($option, '1', $id); break;
     case "unpublish": publish($option, '0', $id); break;
     case 'orderup':
		orderExIcons( intval( $id[0] ), -1, $option );
		break;

	case 'orderdown':
		orderExIcons( intval( $id[0] ), 1, $option );
		break;

  }
  function listItems($option) {
  	synchronizeme();
     global $database, $mainframe, $mosConfig_list_limit;

    $limit 		= intval( $mainframe->getUserStateFromRequest( "viewlistlimit", 'limit', $mosConfig_list_limit ) );
	$limitstart = intval( $mainframe->getUserStateFromRequest( "view{$option}limitstart", 'limitstart', 0 ) );


     $database->setQuery("SELECT COUNT(*) FROM #__extension_icons");
     $total = $database->loadResult();

 	require_once( $GLOBALS['mosConfig_absolute_path'] . '/administrator/includes/pageNavigation.php' );
	$pageNav = new mosPageNav( $total, $limitstart, $limit  );

     $database->setQuery("SELECT * FROM #__extension_icons"
                         ." ORDER BY ordering", $pageNav->limitstart, $pageNav->limit );
     $rows = $database->loadObjectList();

     HTML_ExtensionIcons::listItems($option, $rows, $pageNav);
     HTML_ExtensionIcons::printFooter();
}

function edit($option, $uid) {
     global $database;
     $row = new mosExtensionIcons($database);
     $row->load($uid);
     HTML_ExtensionIcons::edit( $option, $row);
     HTML_ExtensionIcons::printFooter();
  }
  function save($option) {
     global $database;
     $row = new mosExtensionIcons($database);
     $row->bind($_POST);
     $row->store();
     mosRedirect("index2.php?option=$option", "Saved");
  }

  function save_silent($option) {
     global $database;
     $row = new mosExtensionIcons($database);
     $row->bind($_POST);
     $row->store();
//     mosRedirect("index2.php?option=$option", "Saved");
  }

  function delete( $option, $cid ) {
     global $database;
     $cids = implode( ',', $cid );
     $database->setQuery( "DELETE FROM #__schichtplan "
                         ."WHERE id IN ($cids)" );
     $database->query();
     mosRedirect( "index2.php?option=$option" );
  }
  function publish( $option, $publish ,$cid ) {
     global $database;
    $cids = implode( ',', $cid );
     $database->setQuery( "UPDATE #__extension_icons "
                         ."SET published='$publish' "
                         ."WHERE id IN ($cids)" );
     $database->query();
     echo $option . '  - ';
     print_r($cids);
     //die();
     mosRedirect( "index2.php?option=$option" );
  }

  function orderExIcons( $uid, $inc, $option )
  {
	global $database;
	$row = new mosExtensionIcons( $database );
	$row->load( (int)$uid );
	$row->updateOrder();
	$row->move( $inc, "published >= 0" );
	$row->updateOrder();

	mosRedirect( "index2.php?option=$option" );
	}

  function synchronizeme ()
  {
  	 global $database;
     echo 'Datenbank synchronisieren... ';
     $database->setQuery("SELECT * FROM #__extension_icons"
                         ." ORDER BY id");
     $backendicons = $database->loadObjectList();

     $database->setQuery("SELECT * FROM #__components"
     					." WHERE admin_menu_link != ''"
                         ." ORDER BY id");
     $db_components = $database->loadObjectList();
	$cnt_new = 0;
     foreach ($db_components as $db_component)
     {
     	$found = false;
     	if ( count($db_components > 0))
     	{
	     	foreach ($backendicons as $backendicon)
   		  	{
				if ( $backendicon->admin_menu_link == $db_component->admin_menu_link )
				{
					$found = true;
				}
     		}
    	 }
    	 if ($found == false)
    	 {
    	 $parent_name = '';
    	 if ($db_component->parent > 0)
    	 {
    	 	$query = "SELECT name FROM #__components WHERE id = ".$db_component->parent;
    	 	//echo $query;
    	 	$database->setQuery($query);
    	 	$parent_name = $database->loadResult();
    	 	//echo $parent_name;
    	 	//die();
    	 }
$query = "INSERT INTO #__extension_icons"
		."\n SET name = '$db_component->name',"
		."\n parent = $db_component->parent,"
		."\n admin_menu_link = '$db_component->admin_menu_link',"
		."\n admin_menu_alt = '$db_component->admin_menu_alt',"
		."\n `option` = '$db_component->option',"
		."\n admin_menu_img = '$db_component->admin_menu_img'";
		if ($parent_name != '')
		{
			$query .= ",\n parent_name = '$parent_name'";
		}

    	 	//echo $query.'<br />';
    	 $database->setQuery($query);
         $database->query();

    	 $cnt_new += 1;

    	 }
     }
	echo $cnt_new.' neue Eintr&auml;ge - OK ';
	echo '<img src="images/tick.png" />';
  }
?>
